﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using ATM_Pro.Module;

namespace ATM_Pro
{
    public class StatePacketHandle : IPacketHandler
    {

        public event ImageDataChangeHandle DataChange;

        StatePacketBean spb = null;

        public StatePacketHandle()
        {
            spb = new StatePacketBean();
        }

        public StatePacketBean StatePackBean
        {
            get { return spb; }
        }

        #region IPacketHandler Members

        public bool CanHandle(byte[] bytes)
        {
            return (IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 8)) == 5);
        }

        public void Handle(byte[] bytes)
        {
            int packetType = -1;
            DateTime sysTime;
            int packetLength = -1;
            int cpuSeizureRate;
            int memorySeizureRate;
            int atmId = -1;
            int horCamState = -1;
            int vecCamState = -1;
            //int aliCamState = -1;

            packetLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 4));
            packetType = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 8));
            sysTime = GetLocalTime(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 12)));
            cpuSeizureRate = bytes[16];
            memorySeizureRate = bytes[17];
            atmId = bytes[18];
            horCamState = bytes[19];
            vecCamState = bytes[20];

            OnDataChanged(this, new DataChangeEventArgs(this.GetType().Name));
        }

        #endregion

        protected void OnDataChanged(object sender, DataChangeEventArgs e)
        {
            if (this.DataChange != null)
            {
                DataChange(sender, e);
            }
        }

        protected DateTime GetLocalTime(int second)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(second).ToLocalTime();
        }

    }
}
